package com.hrp.service;

import com.hrp.common.ChinesePinyinUtil;
import com.hrp.domain.*;
import com.hrp.hrpdao.JjfactDao;
import com.hrp.hrpdao.JxkhzbDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/8/15 0015.
 */
@Service
public class JjfactService {

    @Autowired
    private JjfactDao jjfactDao;

    @Autowired
    private JxkhzbDao jxkhzbDao;

    public List<Tree> getAllJjfact(){
        List<Jjfact> jjfactList = jjfactDao.getAllJjfact();

        List<Tree> resTree = new ArrayList<>();

        Tree tree = new Tree();
        tree.setId("-1");
        tree.setText("全部绩效指标");

        Tree ctree = null;
        List<Tree> cChildrenList = new ArrayList<>();

        for(Jjfact jjfact:jjfactList) {

            ctree = new Tree();
            ctree.setId(jjfact.getId().toString());
            ctree.setText(jjfact.getJjmc());
            cChildrenList.add(ctree);
        }
        tree.setChildren(cChildrenList);
        resTree.add(tree);
        return resTree;
    }


    public boolean updateJjgsmb(String gsmb,Integer jjzbid,String gsHtml){
        String[] gsarr= gsmb.split(",");
        StringBuilder actureGs = new StringBuilder();
        StringBuilder factGs = new StringBuilder();
        try{
            for(int i=0 ;i<gsarr.length;i++){
                Jxkhzb jxkhzb = jxkhzbDao.getZblistByName(gsarr[i]);
                if(jxkhzb==null){
                    actureGs.append(gsarr[i]);
                    factGs.append(gsarr[i]);
                }else{
                    actureGs.append(jxkhzb.getZbmc());
                    factGs.append(jxkhzb.getCloumName());
                }

            }
            Jjfact jjfact = new Jjfact();
            jjfact.setId(jjzbid);
            jjfact.setJjgsmb(factGs.toString());
            jjfact.setJjgsmcHtml(gsHtml);
            jjfact.setJjgsmc(actureGs.toString());
            jjfactDao.updateJjgsmbByJjzb(jjfact);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    public String getJjgsmb(Integer id){
        Jjfact jjfact = jjfactDao.getJjgsmb(id);
        if(jjfact==null){
            return "";
        }else{
            return jjfact.getJjgsmcHtml();
        }
    }

    public List<Jjfact> getAllJJfact(){
        return jjfactDao.getAllJJfact();
    }

    public Integer getJjfactId(){
        return jjfactDao.getJjfactId();
    }

    public void saveJjfactData(Jjfact jjfact){
        jjfact.setJjmcpy(ChinesePinyinUtil.getPinYinHeadChar(jjfact.getJjmc()));
        Jjfact jjfact1= jjfactDao.getJjfactById(jjfact.getId());
        if(jjfact1==null){
            jjfactDao.saveJjfactData(jjfact);
        }else{
            jjfactDao.updateJjfactData(jjfact);
        }
    }
}
